package com.example.qsindusty.jmm_spline.ChooseDriver;

import com.example.qsindusty.jmm_spline.ChooseDriver.ChooseDContact.Presenter;
import com.example.qsindusty.jmm_spline.api.ApiManager;
import com.yixin.ystartlibrary.base.BasePresenter;
import com.yixin.ystartlibrary.base.IBaseView;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administration on 2018/4/2.
 */

public class ChoosePresenter extends BasePresenter implements Presenter {
        ChooseDContact.View mView ;
    @Override
    public void bindView(IBaseView view) {
            mView= (ChooseDContact.View) view;
    }

    @Override
    public void unbindView() {
            dispose();
    }

    @Override
    public void getDriver(String token,String row ,String page) {
        ApiManager.getInstence().getJmmApi().getFreeDriver(token,row,page,null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DriverData>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(DriverData data) {
                        if (data.getCount()!=0)
                            mView.getSuccessful(data);
                        else
                            mView.getNomore(data.getMessage());
                    //    mView.getSuccessful(data);

                    }

                    @Override
                    public void onError(Throwable e) {
                    mView.closeLoading();
                    mView.getFrailer(e.getMessage().toString());
                    }

                    @Override
                    public void onComplete() {
                        mView.closeLoading();
                    }
                });
    }

    @Override
    public void getDriver(String token, String row, String page, String name) {
        ApiManager.getInstence().getJmmApi().getFreeDriver(token,row,page,name)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DriverData>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(DriverData data) {
                        if (data.getCount()!=0)
                            mView.searchSuccessful(data);
                        else
                            mView.searchNomore(data.getMessage());


                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.closeLoading();
                        mView.searchFrailer(e.getMessage().toString());
                    }

                    @Override
                    public void onComplete() {
                        mView.closeLoading();
                    }
                });

    }
}
